package com.hudson.core.concurrent.threadpool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.locks.ReentrantLock;

public class WatchList<T> {

    List<T> list = new ArrayList<>();
    List<T> historyList = new ArrayList<>();
    ReentrantLock lock = new ReentrantLock();

    public void add(T data) {
        lock.lock();
        try {
            list.add(data);
            historyList.add(data);
        } finally {
            lock.unlock();
        }
    }

    public T get(int index) {
        lock.lock();
        try {
            return list.get(index);
        } finally {
            lock.unlock();
        }
    }

    public <U> U call(Callable<U> callable) throws Exception {
        lock.lock();
        try {
            return callable.call();
        } finally {
            lock.unlock();
        }
    }

    public int size() {
        lock.lock();
        try {
            return list.size();
        } finally {
            lock.unlock();
        }
    }

    public int historySize() {
        lock.lock();
        try {
            return historyList.size();
        } finally {
            lock.unlock();
        }
    }
}
